﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Gezel.Parser
{
  public class Set<T> : ICollection<T>
  {
    List<T> content = new List<T>();

    public Set()
    {
    }

    public Set(T t)
    {
      Add(t);
    }

    public Set(params T[] list)
    {
      AddRange(list);
    }

    public bool IsReadOnly
    {
      get
      {
        return false;
      }
    }

    public bool IsEmpty
    {
      get
      {
        return Count == 0;
      }
    }

    public int Count
    {
      get
      {
        return content.Count;
      }
    }

    public void Clear()
    {
      content.Clear();
    }

    public void CopyTo(T[] array, int index)
    {
      content.CopyTo(array, index);
    }

    public void Add(T item)
    {
      if (!content.Contains(item))
        content.Add(item);
    }

    public void AddRange(T[] list)
    {
      foreach (T t in list)
        Add(t);
    }

    public bool Remove(T item)
    {
      if (content.Contains(item))
        return content.Remove(item);
      return false;
    }

    public void Remove(params T[] items)
    {
      foreach (T item in items)
        Remove(item);
    }

    public void Remove(Set<T> set)
    {
      foreach (T item in set)
        Remove(item);
    }

    public bool Contains(T item)
    {
      return content.Contains(item);
    }

    public T this[int index]
    {
      get
      {
        return content[index];
      }
    }

    public Set<T> Union(T tok)
    {
      Set<T> union = Clone();
      union.Add(tok);
      return union;
    }

    public Set<T> Union(Set<T> set)
    {
      Set<T> union = Clone();
      foreach (T t in set)
        union.Add(t);

      return union;
    }

		public Set<T> Union(params T[] tokens)
		{
			Set<T> union = Clone();
			foreach (T t in tokens)
				union.Add(t);

			return union;
		}

    public Set<T> Clone()
    {
      Set<T> clone = new Set<T>();
      foreach (T t in content)
        clone.Add(t);

    

      return clone;
    }

    public System.Collections.IEnumerator GetEnumerator()
    {
      return content.GetEnumerator();     
    }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
      return content.GetEnumerator();
    } 

    public override string ToString()
    {
      StringBuilder str = new StringBuilder();
      str.Append("{");

      foreach (T item in content)
        str.Append(item.ToString() + ", ");

      // remove last comma and whitespace
      str.Remove(str.Length - 2, 2);
      str.Append("}");

      return str.ToString();
    }
  }
}
